﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;
 using emp.Model;
using  emp.IDAL;
using emp.BLL;
 using emp.ProcessFlow;
using emp.Model.SysManage;
public partial class basedata_Task_Schedule_TaskScheduleSetupModify : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            TaskBasic tb = new TaskBasic();
            IList list = tb.getTask_Basic();
            TaskBasicInfo cellInfo = null;
            int m = list.Count;
            IEnumerator enumerator1 = list.GetEnumerator();
            enumerator1.MoveNext();
            int n;
            for (n = 1; n <= m; n++)
            {
                cellInfo = (TaskBasicInfo)enumerator1.Current;
                this.DropDownList1.Items.Add(new ListItem(cellInfo.TBI_VALUE, cellInfo.TBI_ID.ToString()));
                enumerator1.MoveNext();
            }

            int tss_id = 0;

            if (Request.QueryString["id"] != null)
            {
                tss_id = int.Parse(Request.QueryString["id"]);
            }
            
            TaskScheduleSetup tss = new TaskScheduleSetup();
            TaskScheduleSetupInfo tssinfo = tss.getTask_Schedule_StepInfo(tss_id);
            this.DropDownList1.SelectedValue = tssinfo.TSS_TBI_ID.ToString();
            string tbi_type = tssinfo.TSS_TYPE.ToString();

            this.TextBox1.Text = tssinfo.TSS_START_MINUTE.ToString();
            this.TextBox2.Text = tssinfo.TSS_START_SECOND.ToString();

            if (tbi_type.EndsWith("D"))
            {
                this.RadioButton1.Checked = true;
            }
            if (tbi_type.EndsWith("W"))
            {
                this.RadioButton2.Checked = true;
                this.Panel1.Visible = true;

                int weekday = tssinfo.TSS_WEEK_DAY;
                if (weekday == 1) { this.RadioButton4.Checked = true; }
                if (weekday == 2) { this.RadioButton5.Checked = true; }
                if (weekday == 3) { this.RadioButton6.Checked = true; }
                if (weekday == 4) { this.RadioButton7.Checked = true; }
                if (weekday == 5) { this.RadioButton8.Checked = true; }
                if (weekday == 6) { this.RadioButton9.Checked = true; }
                if (weekday == 7) { this.RadioButton10.Checked = true; }
                
            }
            if (tbi_type.EndsWith("M"))
            {
                this.RadioButton3.Checked = true;
                this.panel2.Visible = true;
                int monthday = tssinfo.TSS_MONTH_DAY;
                this.txt_day.Text = monthday.ToString();
            }

            string state = tssinfo.TSS_STATUS;
            if (state.EndsWith("Y"))
            {
                this.ch_state.Checked = true;
            }
            else
            {
                this.ch_state.Checked = false;
            }


        }
    }

  
    protected void ImageButton1_Click(object sender, EventArgs e)
    {

        int tss_id = 0;

        if (Request.QueryString["id"] != null)
        {
            tss_id = int.Parse(Request.QueryString["id"]);
        }

        int tsstab_id = int.Parse(this.DropDownList1.SelectedValue);
        string tbi_type = null;
        int weekday = 0;
        int monthday = 0;
        if (this.RadioButton1.Checked == true)
        {
            tbi_type = "D";
        }
        if (this.RadioButton2.Checked == true)
        {
            tbi_type = "W";
            if (this.RadioButton4.Checked == true)
            {
                weekday = 1;
            }
            if (this.RadioButton5.Checked == true)
            {
                weekday = 2;
            }
            if (this.RadioButton6.Checked == true)
            {
                weekday = 3;
            }
            if (this.RadioButton7.Checked == true)
            {
                weekday = 4;
            }
            if (this.RadioButton8.Checked == true)
            {
                weekday = 5;
            }
            if (this.RadioButton9.Checked == true)
            {
                weekday = 6;
            }
            if (this.RadioButton10.Checked == true)
            {
                weekday = 7;
            }
        }
        if (this.RadioButton3.Checked == true)
        {
            tbi_type = "M";
            monthday = int.Parse(this.txt_day.Text);
        }
        int minute = int.Parse(this.TextBox1.Text);
        int second = int.Parse(this.TextBox2.Text);
        if (HttpContext.Current.Session["USER_KEY"] == null)
        {
            Response.Write("<script language=javascript>window.parent.parent.location.href=\"../../logon.aspx\";</script>");
            return;
        }
        int userid = ((UserInfo)Session["USER_KEY"]).UserID;
        string state = null;

        if (this.ch_state.Checked == true)
        {
            state = "Y";
        }
        else
        {
            state = "N";
        }
        TaskScheduleSetupInfo tssinfo = new TaskScheduleSetupInfo(tsstab_id, tbi_type, minute, second, weekday, monthday, state, userid);
        TaskScheduleSetupController tss = new TaskScheduleSetupController();
        tss.Update(tssinfo,tss_id);

    }


    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        this.Panel1.Visible = true;
        this.panel2.Visible = false;
        this.RadioButton4.Checked = true;

    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        this.Panel1.Visible = false;
        this.panel2.Visible = false;
    }
    protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
    {
        this.panel2.Visible = true;
        this.Panel1.Visible = false;
    }
    protected void ch_state_CheckedChanged(object sender, EventArgs e)
    {
        if (this.ch_state.Checked== true)
        {
            int tbi_id =int.Parse( this.DropDownList1.SelectedValue);
            TaskScheduleSetup tss = new TaskScheduleSetup();
            if (tss.is_Task_One(tbi_id) > 0)
            {

               // Response.Write("<script>alert('此任务以存在一个有效的');</script>");
                this.Label1.Text = "此任务以存在一个有效的";
                this.ch_state.Checked = false;
            }
        }
    }
}
